package fanfou4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import fanfou4j.http.MornHttpClient;

public class FanfouSupport {
	public static final String BASEURL="http://api.fanfou.com";
	protected final DefaultHttpClient httpClient = MornHttpClient.getHttpclient();
	private final AuthScope mAuthScope = new AuthScope("api.fanfou.com", 80);;

	public void setProxy(String host, int port) {
		if (host == null || host.length() == 0 || port == 0) {
			httpClient.getParams().removeParameter(ConnRoutePNames.DEFAULT_PROXY);
			
		} else {
			HttpHost httpHost = new HttpHost(host, port);
			httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
					httpHost);
		}
	}

	public void setCredentials(String username, String password) {
		if (username == null || username.length() == 0 || password == null
				|| password.length() == 0) {
			httpClient.getCredentialsProvider().clear();
		} else {
			httpClient.getCredentialsProvider().setCredentials(mAuthScope,
					new UsernamePasswordCredentials(username, password));
		}
	}
	
	public String doHttpGet(String url, NameValuePair... nameValuePairs) throws Exception{
		url=BASEURL+url;
		HttpGet httpGet= createHttpGet(url, nameValuePairs);
		HttpResponse response = httpClient.execute(httpGet);
		HttpEntity entity = response.getEntity();
		return EntityUtils.toString(entity);
	}
	
	public String doHttpPost(String url,NameValuePair... nameValuePairs) throws Exception{
		url=BASEURL+url;
		HttpPost httpPost=createHttpPost(url, nameValuePairs);
		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		return EntityUtils.toString(entity);
	}
	
	
	protected HttpGet createHttpGet(String url, NameValuePair... nameValuePairs) {
		String query = URLEncodedUtils.format(stripNulls(nameValuePairs),
				HTTP.UTF_8);
		HttpGet httpGet = new HttpGet(url + "?" + query);
		return httpGet;
	}

	protected HttpPost createHttpPost(String url, NameValuePair... nameValuePairs) {
		HttpPost httpPost = new HttpPost(url);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(
					stripNulls(nameValuePairs), HTTP.UTF_8));
		} catch (UnsupportedEncodingException e1) {
			throw new IllegalArgumentException(
					"Unable to encode http parameters.");
		}
		return httpPost;
	}

	private List<NameValuePair> stripNulls(NameValuePair... nameValuePairs) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		for (int i = 0; i < nameValuePairs.length; i++) {
			NameValuePair param = nameValuePairs[i];
			if (param.getValue() != null) {
				params.add(param);
			}
		}
		return params;
	}

}
